﻿/*
 * Author:xmfdsh 11届网络部--邓世豪
 * DataTime：2014-6-9
 * Copyright © 2014 大学生勤工助学服务队 Powered by xmfdsh & 四小狼工作室.
 * 版权所有，违者必究
 */
using QGZX.Helper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;

namespace QGZX.Logic.OA
{
    public class SysController : Controller
    {
        #region 系统管理 管理员列表页面 + Admin()
        /// <summary>
        /// 系统管理 管理员列表页面
        /// </summary>
        /// <returns></returns>
        public ActionResult Admin()
        {
            MODEL.qgzx_config model = OperateContext.Current.BLLSession.Iqgzx_configBLL.GetListBy(x => x.SystemName != null).FirstOrDefault();
            ViewBag.configModel = model;

            List<MODEL.qgoa_admin> list = OperateContext.Current.BLLSession.Iqgoa_adminBLL.GetListBy(x => x.Id != null);
            ViewBag.List = list;
            return View();
        } 
        #endregion

        #region Action 处理程序 + ActionResult Action()
        /// <summary>
        /// Action 处理程序
        /// </summary>
        /// <returns></returns>
        public ActionResult Action()
        {
            string dowhat = Request.Form["dowhat"];
            if (dowhat.Equals("create_admin"))
            {
                string Name = Request.Form["Name"];
                string Password = Request.Form["Password"];
                string RoleId = Request.Form["RoleId"];

                string Remark = Request.Form["Remark"];
                MODEL.qgoa_admin admin = new MODEL.qgoa_admin() { Name = Name, Password = Password, RoleId = Convert.ToInt32(RoleId), Remark = Remark };
                if (OperateContext.Current.BLLSession.Iqgoa_adminBLL.Add(admin) > 0)
                {
                    return OperateContext.Current.RedirectAjax("ok", "操作成功", null, "");
                }
                return OperateContext.Current.RedirectAjax("err", "操作失败", null, "");
            }

            if (dowhat.Equals("delete_admin"))
            {
                int Id = Convert.ToInt32(Request.Form["Id"]);
                if (OperateContext.Current.BLLSession.Iqgoa_adminBLL.DelBy(x => x.Id == Id) > 0)
                {
                    return OperateContext.Current.RedirectAjax("ok", "操作成功", null, "");
                }
                return OperateContext.Current.RedirectAjax("err", "操作失败", null, "");
            }

            if (dowhat.Equals("update_admin"))
            {
                int Id = Convert.ToInt32(Request.Form["Id"]);
                string Name = Request.Form["Name"];
                string Password = Request.Form["Password"];
                string RoleId = Request.Form["RoleId"];
                string Remark = Request.Form["Remark"];
                MODEL.qgoa_admin admin = new MODEL.qgoa_admin() { Name = Name, Password = Password, RoleId = Convert.ToInt32(RoleId), Remark = Remark };
                if (OperateContext.Current.BLLSession.Iqgoa_adminBLL.ModifyBy(admin, x => x.Id ==Id,  "Name", "Password", "RoleId", "Remark") > 0)
                {
                    return OperateContext.Current.RedirectAjax("ok", "操作成功", null, "");
                }
                return OperateContext.Current.RedirectAjax("err", "操作失败", null, "");
            }
            return OperateContext.Current.RedirectAjax("err", "操作失败", null, "");
        } 
        #endregion

        #region 新增 方法 + ActionResult Create()
        /// <summary>
        /// 新增方法
        /// </summary>
        /// <returns></returns>
        public ActionResult Create()
        {
            MODEL.qgzx_config model = OperateContext.Current.BLLSession.Iqgzx_configBLL.GetListBy(x => x.SystemName != null).FirstOrDefault();
            ViewBag.configModel = model;
            List<MODEL.qgoa_role> list = OperateContext.Current.BLLSession.Iqgoa_roleBLL.GetListBy(x => x.Id != null);
            ViewBag.role_list = list;
            return View();
        } 
        #endregion

        #region 编辑方法 + ActionResult Edit()
        /// <summary>
        /// 编辑方法
        /// </summary>
        /// <returns></returns>
        public ActionResult Edit()
        {
            MODEL.qgzx_config model = OperateContext.Current.BLLSession.Iqgzx_configBLL.GetListBy(x => x.SystemName != null).FirstOrDefault();
            ViewBag.configModel = model;
            List<MODEL.qgoa_role> list = OperateContext.Current.BLLSession.Iqgoa_roleBLL.GetListBy(x => x.Id != null);
            ViewBag.role_list = list;
            int Id = Convert.ToInt32(Request["id"]);
            if (Id != null)
            {
                MODEL.qgoa_admin admim = OperateContext.Current.BLLSession.Iqgoa_adminBLL.GetListBy(x => x.Id == Id).FirstOrDefault();
                ViewBag.admin = admim;
            }
            return View();
        }
        #endregion

        #region 系统管理 登陆日志管理 + ActionResult Log()
        /// <summary>
        /// 系统管理 登陆日志管理
        /// </summary>
        /// <returns></returns>
        public ActionResult Log()
        {
            MODEL.qgzx_config model = OperateContext.Current.BLLSession.Iqgzx_configBLL.GetListBy(x => x.SystemName != null).FirstOrDefault();
            ViewBag.configModel = model;
            List<MODEL.qgoa_log> list = OperateContext.Current.BLLSession.Iqgoa_logBLL.GetListBy(x => x.Id != null);
            ViewBag.list = list;
            return View();
        } 
        #endregion

        #region 角色 + ActionResult Role()
        /// <summary>
        /// 角色
        /// </summary>
        /// <returns></returns>
        public ActionResult Role()
        {
            MODEL.qgzx_config model = OperateContext.Current.BLLSession.Iqgzx_configBLL.GetListBy(x => x.SystemName != null).FirstOrDefault();
            ViewBag.configModel = model;
            List<MODEL.qgoa_role> list = OperateContext.Current.BLLSession.Iqgoa_roleBLL.GetListBy(x => x.Id != null);
            ViewBag.role_list = list;
            return View();
        } 
        #endregion

        #region 添加角色页面 + CreateRole()
        /// <summary>
        /// 添加角色页面
        /// </summary>
        /// <returns></returns>
        public ActionResult CreateRole()
        {
            MODEL.qgzx_config model = OperateContext.Current.BLLSession.Iqgzx_configBLL.GetListBy(x => x.SystemName != null).FirstOrDefault();
            ViewBag.configModel = model;
            List<MODEL.qgoa_role> list = OperateContext.Current.BLLSession.Iqgoa_roleBLL.GetListBy(x => x.Id != null);
            ViewBag.role_list = list;
            return View();
        } 
        #endregion

        #region 编辑角色页面 + ctionResult EditRole()
        /// <summary>
        /// 编辑角色页面
        /// </summary>
        /// <returns></returns>
        public ActionResult EditRole()
        {
            MODEL.qgzx_config model = OperateContext.Current.BLLSession.Iqgzx_configBLL.GetListBy(x => x.SystemName != null).FirstOrDefault();
            ViewBag.configModel = model;
            int Id = Convert.ToInt32(Request["id"]);
            if (Id != null)
            {
                MODEL.qgoa_role role = OperateContext.Current.BLLSession.Iqgoa_roleBLL.GetListBy(x => x.Id == Id).FirstOrDefault();
                ViewBag.role = role;
            }
            return View();
        } 
        #endregion

        #region 角色处理程序方法 + ActionResult ActionRole()
        /// <summary>
        /// 角色处理程序方法
        /// </summary>
        /// <returns></returns>
        public ActionResult ActionRole()
        {
            string dowhat = Request.Form["dowhat"];


            if (dowhat == "create_role")
            {
                string Name = Request.Form["Name"];
                bool ForSetting = Request.Form["ForSetting"] == null ? false : true;
                bool ForServer = Request.Form["ForServer"] == null ? false : true; ;
                bool ForUser = Request.Form["ForUser"] == null ? false : true; ;
                bool ForSys = Request.Form["ForSys"] == null ? false : true; ;
                string Remark = Request.Form["Remark"];

                MODEL.qgoa_role role = new MODEL.qgoa_role() { Name = Name, ForSetting = ForSetting, ForServer = ForServer, ForUser = ForUser,ForSys=ForSys,Remark=Remark };
                if (OperateContext.Current.BLLSession.Iqgoa_roleBLL.Add(role) > 0)
                {
                    return OperateContext.Current.RedirectAjax("ok", "操作成功", null, "");
                }
                return OperateContext.Current.RedirectAjax("err", "操作失败", null, "");
            }

            if (dowhat == "update_role")
            {
                int Id = Convert.ToInt32(Request.Form["Id"]);
                string Name = Request.Form["Name"];
                bool ForSetting = Request.Form["ForSetting"] == null ? false : true;
                bool ForServer = Request.Form["ForServer"] == null ? false : true; ;
                bool ForUser = Request.Form["ForUser"] == null ? false : true; ;
                bool ForSys = Request.Form["ForSys"] == null ? false : true; ;
                string Remark = Request.Form["Remark"];

                MODEL.qgoa_role role = new MODEL.qgoa_role() { Name = Name, ForSetting = ForSetting, ForServer = ForServer, ForUser = ForUser, ForSys = ForSys, Remark = Remark };
                if (OperateContext.Current.BLLSession.Iqgoa_roleBLL.ModifyBy(role, x => x.Id ==Id, "Name", "ForSetting", "ForServer", "ForUser", "ForSys", "Remark") > 0)
                {
                    return OperateContext.Current.RedirectAjax("ok", "操作成功", null, "");
                }
                return OperateContext.Current.RedirectAjax("err", "操作失败", null, "");
            }

            if (dowhat == "delete_role")
            {
                int Id = Convert.ToInt32(Request.Form["Id"]);
                if (OperateContext.Current.BLLSession.Iqgoa_roleBLL.DelBy(x => x.Id == Id) > 0)
                {
                    return OperateContext.Current.RedirectAjax("ok", "操作成功", null, "");
                }
                return OperateContext.Current.RedirectAjax("err", "操作失败", null, "");
            }
            return OperateContext.Current.RedirectAjax("err", "操作失败", null, "");
        } 
        #endregion
    }
}
